<?php
class noten_controller extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		if($this->session->userdata('logged_in'))
   		{
			$this->load->model('noten_model');
		}
   		else
   		{
     		//If no session, redirect to login page
     		redirect('welcome', 'refresh');
   		}
	}

	public function index()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'lehrer'))
   		{
			$data['noten'] = $this->noten_model->get_noten();
	
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('noten/notenindex_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	
	public function create_view()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'lehrer'))
   		{
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('noten/notencreate_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	
	public function create()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Code', 'Code', 'required');														   

		if ($this->form_validation->run() === FALSE)
		{
			$this->create_view();
		}
		else
		{
			$this->noten_model->set_noten();	
			$this->index();
		}
	}
	
	public function validate_view()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'lehrer'))
   		{
			$notenid = $this->input->post('notencode');
			$data['noten'] = $this->noten_model->get_noten_id($notenid);
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('noten/notenvali_view', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	
	public function validate()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');

		$this->form_validation->set_rules('Beschreibung', 'Beschreibung', 'required');														   

		if ($this->form_validation->run() === FALSE)
		{	
			$this->validate_view();
		}
		else
		{
			$this->noten_model->validate_noten($this->input->post('notencode'));
			$this->index();
		}	 
	}

	public function klausurfragen_view()
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'lehrer'))
   		{
			$data['notenid'] = $this->input->post('notenid');
			$data['anzahlfragen'] = $this->input->post('anzahlfragen');
			#$data['noten'] = $this->noten_model->get_noten_id($notenid);
			$data['fragen'] = $this->fragen_model->get_fragen();
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('noten/notenfragen_view2', $data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
	
	public function set_klausurfragen()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		#$klausurfragen['notenid'] = $this->input->post('notenid');
		$klausurfragen = $_POST['fragenid'];

		$this->notenfragen_model->set_notenfragen($data);	
		$this->index();

		print_r($_POST);
	}

	public function notenlistview($page = 0)
	{
		$user_data = $this->session->userdata('logged_in');
   		if($this->session->userdata('logged_in') && ($user_data['role'] == 'lehrer'))
   		{	
			$limit = 10;
			$data['noten'] = $this->noten_model->get_all($limit, $page * $limit);
			$data['pages'] = ceil($this->noten_model->get_count() / $limit);
			
			$data['_scripts'] = '';
		 	$data['_styles'] = '';
			$data['head'] = '';
			$data['schulname'] = 'Berufskolleg Uerdingen';
			$data['navi'] = $this->load->view('login/lehrernavi_view', $data,TRUE);
			$data['login'] = '';
			$data['content'] = $this->load->view('noten/notenlist_view',$data,TRUE);
			$data['footer'] = $this->load->view('templates/footer', $data, TRUE);
			
			$this->load->view('MeinBKU_Template/MeinBKU_Template/template', $data);
		}
		else
	 	{
	   		//If no session, redirect to login page
	   		show_error('403 FORBIDDEN',$status_code= 403);
	 	}
	}
}